<template>
  <div>
    <!-- 仓库维护-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjWarehouseMaintenance"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="isOpen != 2">启 用</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(3)" :disabled="isOpen != 1">停 用</multiuse-button>
      </template>
    </FilterTable>

    <BaseDialog :visible.sync="isAdd" :title="'新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addModel :rowData="formData" @close="addcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isEmit" :title="'编辑'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getDetail, postlocked, getWarehouseList } from '@/api/omSale/HeadquartersInventory/WarehouseMaintenance/index.js';
import { dictionary } from '@/utils/dictMixin';
import addModel from './addModel';
import emitModel from './emitModel';
export default {
  name: 'WarehouseMaintenance',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        xhrAbort: true
      },
      formData: {},
      isAdd: false,
      isEmit: false,
      isOpen: 0,
      selectedList: []
    };
  },
  mixins: [dictionary],
  components: {
    addModel,
    emitModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getLists();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '仓库名称',
          fieldName: 'warehouseName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '仓库类型',
          fieldName: 'warehouseType',
          placeholder: '请选择',
          itemList: this.createDictList('6009') || []
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003') || []
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'beginCreatedAt|endCreatedAt',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '上级仓库',
          fieldName: 'parWarehouseId',
          placeholder: '请选择',
          itemList: []
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          editItems: this.createDictList('1003')
        },
        {
          title: '上级仓库',
          dataIndex: 'parWarehouseId',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editItems: []
        },
        {
          title: '仓库代码',
          dataIndex: 'warehouseCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库名称',
          dataIndex: 'warehouseName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '仓库类型',
          dataIndex: 'warehouseType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6009'),
          editItems: this.createDictList('6009')
        },
        {
          title: '配车优先级',
          dataIndex: 'matchPriority',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    selectChangeHandle(row) {
      console.log(row, 'row');
      if (row.length > 0) {
        let isOpen = 0;
        let isClose = 0;
        row.map(x => {
          if (x.status == 10031001) {
            isOpen = isOpen + 1;
          } else {
            isClose = isClose + 1;
          }
        });
        if (isOpen > 0 && isClose == 0) {
          this.isOpen = 1;
        } else if (isOpen == 0 && isClose > 0) {
          this.isOpen = 2;
        } else {
          this.isOpen = 0;
        }
        this.selectedList = row;
      } else {
        this.selectedList = [];
        this.isOpen = 0;
      }
    },
    async getLists() {
      let res = await getWarehouseList();
      this.columns.find(x => x.dataIndex == 'parWarehouseId').filterItems = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      this.columns.find(x => x.dataIndex == 'parWarehouseId').editItems = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      this.topFilterList.find(x => x.fieldName == 'parWarehouseId').itemList = res.data.list.map(x => ({ text: x.warehouseName, value: x.warehouseId }));
      console.log(res.data);
    },
    async addHandle(type) {
      if (type == 1) {
        this.isAdd = true;
      } else if (type == 2) {
        await this.$confirm('是否启用?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectedList.map(x => {
              arr.push(x.id);
            });
            let res = await postlocked({ status: 10031001, ids: arr });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '启用成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else if (type == 3) {
        await this.$confirm('是否停用?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectedList.map(x => {
              arr.push(x.id);
            });
            let res = await postlocked({ status: 10031002, ids: arr });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '停用成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.isEmit = true;
    },
    addcloseHandler() {
      this.isAdd = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    emitcloseHandler() {
      this.isEmit = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
